package com.twoscreen;

import java.util.logging.Logger;

import org.json.JSONObject;

import com.google.appengine.api.users.User;
import com.twoscreen.test.Test;
import com.twoscreen.test.TestMethod;

public class Validator {
	private static final Logger log = Logger.getLogger("com.twoscreen.Validator.java");
	

	/** To test a string before storing in database
	 * @param validateType 1:Google URL 2:Standard URL 3:Image URL 4: username
	 * @param input The string to be tested
	 * @return Test status - true if OK
	 */
	public static Boolean validate(int validateType, String input) {
		Boolean test = null;
		switch (validateType) {
		case 1:
			//Google URL
			test = input.matches("^(http|https)://plus.google.com/(\\w+|\\w+/|\\w+.\\w+/)+\\w*");
			break;
			
		case 2:
			//URL
			test = input.matches("^(http|https)://(\\w+.|\\w+.\\w+.)(\\w+|\\w+.\\w+)+\\w+");
			break;
			
		case 3:
			//Image URL
			test = input.matches("^(http|https)://(\\w+.|\\w+.\\w+.)((\\w|-|_)+/|(\\w|-|_)+.(\\w|-|_)+/)+(\\w|-|_)+(.jpg|.png|.svg|.gif|.jpeg)$");
			break;
			
		case 4:
			//User name
			test = input.matches("^[A-z0-9-]+$");
			test = test && input.length()<=30;
			break;
			
		default:
			break;
		}
		
		return test;
	}
	
	/**
	 * Tests a passed Google User ID matches the Google User ID saved in the
	 * data store for the passed viewer ID 
	 * @param user Google User object to be tested
	 * @param viewerId Viewer ID to search the data store for
	 * @return True if the ID's match
	 */
	public static Boolean checkGoogleUserViewerID(User user, String viewerId) {
		Boolean test = false;
		//Get the google ID for the user
		String googleId = user.getUserId();
		log.info("google id: " + googleId);
		//Get the google ID stored for the viewerId
		String viewerGid = TUser.getTUserByViewerId(viewerId).getUser().getUserId();
		log.info("viewer g id: " + viewerGid);
		log.info("match: " + googleId.equals(viewerGid));
		//Test it
		if (googleId.equals(viewerGid)) {
			test = true;
		}
		
		return test;
	}
	
	/**
	 * Runs all the tests on the class
	 * @return Test results JSON
	 */
	public static JSONObject test() {
		Test test = new Test("Validator");
		test.addMethod(testValidate());
		return test.getJSONObject();
	}
	
	/**
	 * Tests the validate method
	 * @return Test results JSON
	 */
	private static JSONObject testValidate() {
		TestMethod tm = new TestMethod("testValidate");
		
		//Test 1
		Boolean test1 = false;
    	String testStack1 = "";
    	try {
    		test1 = (validate(1, "http://plus.google.com/123456/posts"));
    	} catch (Exception e) {
    		testStack1 = e.getMessage();
    	}
		tm.addTest("Google URL valid", test1, testStack1);
		
		//Test 2
		Boolean test2 = false;
    	String testStack2 = "";
    	try {
    		test2 = (validate(1, "http://google.com/123456/posts") == false);
    	} catch (Exception e) {
    		testStack2 = e.getMessage();
    	}
		tm.addTest("Google URL invalid", test2, testStack2);
		
		//Test 3
		Boolean test3 = false;
    	String testStack3 = "";
    	try {
    		test3 = (validate(2, "http://google.com/123456/posts"));
    	} catch (Exception e) {
    		testStack3 = e.getMessage();
    	}
		tm.addTest("URL valid", test3, testStack3);
		
		//Test 4
		Boolean test4 = false;
    	String testStack4 = "";
    	try {
    		test4 = (validate(2, "ssh://google.com/123456/posts") == false);
    	} catch (Exception e) {
    		testStack4 = e.getMessage();
    	}
		tm.addTest("URL invalid", test4, testStack4);
		
		//Test 5
		Boolean test5 = false;
    	String testStack5 = "";
    	try {
    		test5 = (validate(3, "http://google.com/123456/posts.jpg"));
    	} catch (Exception e) {
    		testStack5 = e.getMessage();
    	}
		tm.addTest("Image URL valid", test5, testStack5);
		
		//Test 6
		Boolean test6 = false;
    	String testStack6 = "";
    	try {
    		test6 = (validate(3, "http://google.com/123456!/posts.jpg") == false);
    	} catch (Exception e) {
    		testStack6 = e.getMessage();
    	}
		tm.addTest("Image URL invalid", test6, testStack6);
		
		//Test 7
		Boolean test7 = false;
    	String testStack7 = "";
    	try {
    		test7 = (validate(4, "James"));
    	} catch (Exception e) {
    		testStack7 = e.getMessage();
    	}
		tm.addTest("User name valid", test7, testStack7);
		
		//Test 8
		Boolean test8 = false;
    	String testStack8 = "";
    	try {
    		test8 = (validate(4, "James!") == false);
    	} catch (Exception e) {
    		testStack8 = e.getMessage();
    	}
		tm.addTest("User name invalid", test8, testStack8);
		
    	return tm.getJSONObject();
    }
	
}
